package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.WGoodMapper;
import com.ruoyi.system.mapper.WOrderGoodMapper;
import com.ruoyi.system.mapper.WOrderMapper;
import com.ruoyi.system.mapper.WOrderOpearaGoodMapper;
import com.ruoyi.system.mapper.WOrderOpearaMapper;
import com.ruoyi.system.domain.WGood;
import com.ruoyi.system.domain.WGoodsRecords;
import com.ruoyi.system.domain.WOrder;
import com.ruoyi.system.domain.WOrderGood;
import com.ruoyi.system.domain.WOrderOpeara;
import com.ruoyi.system.domain.WOrderOpearaGood;
import com.ruoyi.system.domain.vo.WOrderOpearaVO;
import com.ruoyi.system.service.IWGoodService;
import com.ruoyi.system.service.IWOrderOpearaService;

/**
 * 物料订单操作记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-05
 */
@Service
public class WOrderOpearaServiceImpl implements IWOrderOpearaService 
{
    @Autowired
    private WOrderOpearaMapper wOrderOpearaMapper;
    @Autowired
    private WOrderMapper wOrderMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private WOrderOpearaGoodMapper wOrderOpearaGoodMapper;
    
    @Autowired
    private IWGoodService IWGoodService;
    
    @Autowired
    private WGoodMapper wGoodMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private WOrderGoodMapper wOrderGoodMapper;
    
    @Transactional
    @Override
    public R addReturn(WOrderOpearaVO wOrderOpearaVO) {
    	String returnExpress = wOrderOpearaVO.getReturnExpress();
    	if(StringUtils.isBlank(returnExpress)) return R.error("没传退回物流公司名字");
    	String returnNo = wOrderOpearaVO.getReturnNo();
    	if(StringUtils.isBlank(returnNo)) return R.error("没传退回单号");
    	Long deptId = wOrderOpearaVO.getDeptId();
    	SysDept selectDeptById = sysDeptMapper.selectDeptById(deptId);
    	wOrderOpearaVO.setDeptName(selectDeptById.getDeptName());
    	
    	
    	List<WOrderOpearaGood> wOrderOpearaGoodList = wOrderOpearaVO.getWOrderOpearaGoodList();
    	if(wOrderOpearaGoodList == null  || wOrderOpearaGoodList.size() == 0 ) return R.error("没传wOrderOpearaGoodList");
    	for (WOrderOpearaGood wOrderOpearaGood : wOrderOpearaGoodList) {
    		Long goodsId = wOrderOpearaGood.getGoodsId();
    		if(goodsId == null ) return R.error("没传goodsId");
    		
    		Long getNumber = wOrderOpearaGood.getNumber();
    		if(getNumber == null  || getNumber.intValue() <=0 ) return R.error("没传数量 number ");
    		
    		WGood selectWGoodById = wGoodMapper.selectWGoodById(goodsId);
    		wOrderOpearaVO.setCustomerId(selectWGoodById.getCustomerId());
    		wOrderOpearaVO.setCustomerName(selectWGoodById.getCustomerName());
    		
    		BeanUtils.copyBeanProp(wOrderOpearaGood, selectWGoodById);
    		wOrderOpearaGood.setNumber(getNumber);
    		wOrderOpearaGood.setGoodsId(goodsId);
    		wOrderOpearaGood.setId(null);
		}
    	wOrderOpearaVO.setType(3);
//    	wOrderOpearaVO.setStatus(3);
    	wOrderOpearaMapper.insertWOrderOpeara(wOrderOpearaVO);
    	
    	
    	
    	for (WOrderOpearaGood wOrderOpearaGood : wOrderOpearaGoodList) {
    		wOrderOpearaGood.setOpearaId(wOrderOpearaVO.getId());
    		wOrderOpearaGoodMapper.insertWOrderOpearaGood(wOrderOpearaGood);
    		WGoodsRecords wGoodsRecords = new WGoodsRecords();
			wGoodsRecords.setChangeNumber( wOrderOpearaGood.getNumber().intValue());
			wGoodsRecords.setIsIn(4);
			wGoodsRecords.setInType(1);
			wGoodsRecords.setGoodsId(wOrderOpearaGood.getGoodsId());
    		IWGoodService.dealWGood(wGoodsRecords);
    	}
    	return R.success();
    }
    @Transactional
    @Override
    public R addIn(WOrderOpearaVO wOrderOpearaVO) {
    	Long id = wOrderOpearaVO.getId();
    	if(id == null ) return R.error("没传id");
    	WOrderOpeara selectWOrderOpearaById = wOrderOpearaMapper.selectWOrderOpearaById(id);
    	Integer getStatus = selectWOrderOpearaById.getStatus();
    	if (getStatus == 3) return R.error("已经入库,不能在入库");
    	if (getStatus != 2) return R.error("状态错误， 不能入库");
    	
    	Long orderId = selectWOrderOpearaById.getOrderId();
    	
    	
    	List<WOrderOpearaGood> wOrderOpearaGoodList = wOrderOpearaVO.getWOrderOpearaGoodList();
    	if(wOrderOpearaGoodList == null  || wOrderOpearaGoodList.size() == 0 ) return R.error("没传wOrderOpearaGoodList");
    	for (WOrderOpearaGood wOrderOpearaGood : wOrderOpearaGoodList) {
    		Long wOrderOpearaGoodId = wOrderOpearaGood.getId();
    		if(wOrderOpearaGoodId == null ) return R.error("没传wOrderOpearaGoodList数组对象里面的id");
    		
    		Long checkNum = wOrderOpearaGood.getCheckNum();
    		if(checkNum == null  || checkNum.intValue() <=0 ) return R.error("没传盘点数量 checkNum ");
    	}
    	wOrderOpearaVO.setType(null);
    	wOrderOpearaVO.setStatus(3);
    	wOrderOpearaMapper.updateWOrderOpeara(wOrderOpearaVO);
    	
    	
    	WOrder selectWOrderById = wOrderMapper.selectWOrderById(orderId);
    	Integer orderStatus = selectWOrderById.getOrderStatus();
    	if (orderStatus.intValue() == 1 ) {
    		selectWOrderById.setOrderStatus(2);
    		wOrderMapper.updateWOrder(selectWOrderById);
    	}
    	
    	for (WOrderOpearaGood wOrderOpearaGood : wOrderOpearaGoodList) {
    		wOrderOpearaGoodMapper.updateWOrderOpearaGood(wOrderOpearaGood);
    		WGoodsRecords wGoodsRecords = new WGoodsRecords();
    		wGoodsRecords.setChangeNumber( wOrderOpearaGood.getCheckNum().intValue());
    		wGoodsRecords.setIsIn(3);
    		wGoodsRecords.setInType(2);
    		wGoodsRecords.setGoodsId(wOrderOpearaGood.getGoodsId());
    		wGoodsRecords.setOrderSn(wOrderOpearaGood.getOrderSn());
    		IWGoodService.dealWGood(wGoodsRecords);
    	}
    	
    	
    	WOrderGood wOrderGood = new WOrderGood();
    	wOrderGood.setOrderId(orderId);
		List<WOrderGood> selectWOrderGoodList = wOrderGoodMapper.selectWOrderGoodList(wOrderGood );
		
		   List<WGood> selectWGoodListByPnList = wGoodMapper.selectWGoodListByPnListNew(selectWOrderGoodList, selectWOrderById.getCustomerId(), selectWOrderById.getDeptId());
	        if (CollectionUtils.isEmpty(selectWGoodListByPnList)) {
	        	selectWGoodListByPnList = new ArrayList<>();
			}
	        
	        Long canNumber = 0l;
	        int i = 0;
	        Long productNumber = selectWOrderById.getProductNumber();
	        for (WOrderGood e : selectWOrderGoodList) {
	        	
	        	Long number = e.getQuantity()   *  productNumber;
	        	e.setNumber(number);
	        	for (WGood wGood : selectWGoodListByPnList) {
					if (wGood.getComment().equals(e.getComment()) && e.getPn().equals(wGood.getPn())
							&& e.getDesignator().equals(wGood.getDesignator())
							&& e.getDimension().equals(wGood.getDimension())
							&& e.getFootprint().equals(wGood.getFootprint())
							&& e.getManufacture().equals(wGood.getManufacture())
							) {
						e.setGoodsId(wGood.getId());
						
						Long number2 = wGood.getNumber();
						
						WOrderGood selectWOrderGoodNum = wOrderGoodMapper.selectWOrderGoodNumNotOrderId(wGood.getId(),orderId);
						Long lockNumber = selectWOrderGoodNum.getNumber();
					
						long canNumber2 = (number2 - lockNumber )  / e.getQuantity()   > productNumber.longValue() ?   productNumber :
								(number2 - lockNumber )  / e.getQuantity()  ;
						 if (i == 0) {
							 canNumber = canNumber2  ;
						}else{
							if (canNumber2 < canNumber ) {
								canNumber = canNumber2;
							}
						}
						 i++;
					}
				}
	        }
	        selectWOrderById.setCanNumber(canNumber);
	        
	  		wOrderMapper.updateWOrder(selectWOrderById);
    	
    	return R.success();
    }
    
	@Override
	public R addWOrderOpeara(WOrderOpearaVO wOrderOpearaVO) {
		Long orderId = wOrderOpearaVO.getOrderId();
		if (orderId == null)
			return R.error("没传订单id");
		WOrder selectWOrderById = wOrderMapper.selectWOrderById(orderId);
		Integer type = wOrderOpearaVO.getType();
		if (type == null)
			return R.error("没传type");
		Date createTime = new Date();
		wOrderOpearaVO.setCustomerId(selectWOrderById.getCustomerId());
		wOrderOpearaVO.setCustomerName(selectWOrderById.getCustomerName());
		wOrderOpearaVO.setDeptId(selectWOrderById.getDeptId());
		wOrderOpearaVO.setDeptName(selectWOrderById.getDeptName());
		wOrderOpearaVO.setCreateTime(createTime);
		wOrderOpearaVO.setOrderSn(selectWOrderById.getOrderSn());
		// wOrderOpearaVO.set
		List<WOrderOpearaGood> wOrderOpearaGoodList = wOrderOpearaVO.getWOrderOpearaGoodList();
		for (WOrderOpearaGood wOrderOpearaGood : wOrderOpearaGoodList) {
			Long goodsId = wOrderOpearaGood.getGoodsId();
			Long number = wOrderOpearaGood.getNumber();
			if (goodsId == null)
				return R.error("没传goodsId");
			if (number == null)
				return R.error("没传number");
			wOrderOpearaGood.setOrderId(orderId);
			wOrderOpearaGood.setOrderSn(selectWOrderById.getOrderSn());
			wOrderOpearaGood.setCustomerId(selectWOrderById.getCustomerId());
			wOrderOpearaGood.setDeptId(selectWOrderById.getDeptId());

			wOrderOpearaGood.setCreateTime(createTime);
		}
		if (type == 1 ) {
			wOrderOpearaVO.setStatus(2);
		}
		wOrderOpearaMapper.insertWOrderOpeara(wOrderOpearaVO);
		wOrderOpearaGoodList.forEach(e -> e.setOpearaId(wOrderOpearaVO.getId()));
		for (WOrderOpearaGood wOrderOpearaGood : wOrderOpearaGoodList) {
			wOrderOpearaGoodMapper.insertWOrderOpearaGood(wOrderOpearaGood);
		}

		return R.success(wOrderOpearaVO);
	}
    
    

    /**
     * 查询物料订单操作记录
     * 
     * @param id 物料订单操作记录主键
     * @return 物料订单操作记录
     */
    @Override
    public R<WOrderOpearaVO> selectWOrderOpearaById(Long id)
    {
    	WOrderOpearaVO wOrderOpearaVO = new WOrderOpearaVO();
        WOrderOpeara selectWOrderOpearaById = wOrderOpearaMapper.selectWOrderOpearaById(id);
        BeanUtils.copyProperties(selectWOrderOpearaById, wOrderOpearaVO);
        Long orderId = selectWOrderOpearaById.getOrderId();
        if (orderId != null ) {
        	WOrder selectWOrderById = wOrderMapper.selectWOrderById(orderId);
        	wOrderOpearaVO.setOrderSn(selectWOrderById.getOrderSn());
        	SysUser selectUserById = sysUserMapper.selectUserById(selectWOrderById.getCustomerId());
        	wOrderOpearaVO.setCustomerPhone(selectUserById.getPhonenumber());
        	Date deliveryTime = selectWOrderById.getDeliveryTime();
        	wOrderOpearaVO.setDeliveryTime(deliveryTime); 
        	wOrderOpearaVO.setProductName(selectWOrderById.getProductName());
		}
        WOrderOpearaGood wOrderOpearaGood = new WOrderOpearaGood();
        wOrderOpearaGood.setOpearaId(id);
		List<WOrderOpearaGood> selectWOrderOpearaGoodList = wOrderOpearaGoodMapper.selectWOrderOpearaGoodList(wOrderOpearaGood );
		wOrderOpearaVO.setWOrderOpearaGoodList(selectWOrderOpearaGoodList);
		return R.success(wOrderOpearaVO);
    }

    /**
     * 查询物料订单操作记录列表
     * 
     * @param wOrderOpeara 物料订单操作记录
     * @return 物料订单操作记录
     */
    @Override
    public List<WOrderOpearaVO> selectWOrderOpearaList(WOrderOpearaVO wOrderOpeara)
    {
        return wOrderOpearaMapper.selectWOrderOpearaListNew(wOrderOpeara);
    }
    @Override
    public List<WOrderOpearaVO> listRefundSmall(WOrderOpearaVO wOrderOpeara)
    {
    	return wOrderOpearaMapper.listRefundSmall(wOrderOpeara);
    }

    /**
     * 新增物料订单操作记录
     * 
     * @param wOrderOpeara 物料订单操作记录
     * @return 结果
     */
    @Override
    public int insertWOrderOpeara(WOrderOpeara wOrderOpeara)
    {
        wOrderOpeara.setCreateTime(DateUtils.getNowDate());
        return wOrderOpearaMapper.insertWOrderOpeara(wOrderOpeara);
    }

    /**
     * 修改物料订单操作记录
     * 
     * @param wOrderOpeara 物料订单操作记录
     * @return 结果
     */
    @Override
    public int updateWOrderOpeara(WOrderOpeara wOrderOpeara)
    {
        wOrderOpeara.setUpdateTime(DateUtils.getNowDate());
        return wOrderOpearaMapper.updateWOrderOpeara(wOrderOpeara);
    }

    /**
     * 批量删除物料订单操作记录
     * 
     * @param ids 需要删除的物料订单操作记录主键
     * @return 结果
     */
    @Override
    public int deleteWOrderOpearaByIds(Long[] ids)
    {
        return wOrderOpearaMapper.deleteWOrderOpearaByIds(ids);
    }

    /**
     * 删除物料订单操作记录信息
     * 
     * @param id 物料订单操作记录主键
     * @return 结果
     */
    @Override
    public int deleteWOrderOpearaById(Long id)
    {
        return wOrderOpearaMapper.deleteWOrderOpearaById(id);
    }
}
